package com.cockybook.poemspider.job;

import com.cockybook.poemspider.beans.Poem;
import com.cockybook.poemspider.beans.Poet;
import com.cockybook.poemspider.dao.PoemsRepository;
import com.cockybook.poemspider.dao.PoetsRepository;
import com.cockybook.poemspider.model.IntroductionModel;
import com.cockybook.utils.Const;
import com.cockybook.utils.JsoupUtil;
import com.cockybook.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 抓取诗歌
 */
@Component
@Order(1)
public class PoemSpider implements CommandLineRunner {

    private final Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    PoemsRepository poemsRepository;

    Map<String ,String> dynastys =new HashMap<String, String>();

    public PoemSpider(){
        this.dynastys.put("xianqinshiren","先秦");
        this.dynastys.put("hanchaoshiren","汉朝");
        this.dynastys.put("weijinshiren","魏晋");
        this.dynastys.put("nanbeichaoshiren","南北朝");
        this.dynastys.put("suichaoshiren","隋");
        this.dynastys.put("tangdaishiren","唐");
        this.dynastys.put("songdaishiren","宋");
        this.dynastys.put("jinchaoshiren","金");
        this.dynastys.put("liaochaoshiren","辽");
        this.dynastys.put("yuanchaoshiren","元");
        this.dynastys.put("mingchaoshiren","明");
        this.dynastys.put("qingchaoshiren","清");
        this.dynastys.put("xiandangdaishiren","近当代");
    }

    @Override
    public void run(String... args) throws Exception {

        if(args==null ||args.length!=2){
            logger.error("args ==2  args: (min max)");
            System.exit(-1);
        }

        int min= Integer.parseInt(args[0]);
        int max= Integer.parseInt(args[1]);
        for (int i = min; i < max; i++) {
            logger.info("start download index : "+ i);
            parseDynasty(String.format(Const.srcURLPoemPREFINX,i),i, true);
        }

//        for (int i = 420000; i < MAXCOUNT; i++) {
//            logger.info("start download index : "+ i);
//            parseDynasty(String.format(Const.srcURLPoemPREFINX,i),i, true);
//        }
    }

    /**
     * 解析朝代url
     * @param dynastyUrl
     * @param isGodownPage  是否下钻
     */
    public void parseDynasty(String dynastyUrl, int id ,boolean isGodownPage) throws Exception {
        Document document =null;
        try {
            document = JsoupUtil.urlGet(Const.srcURL+dynastyUrl);
        }catch (Exception e){
            logger.info("id:"+id , e);
            Poem poem = new Poem();
            poem.setId(id);
            poem.setMemo("Jsoup_get_error");
            addPoem(poem);
            return;
        }

        if(document!=null){
            logger.info("Title: "+document.title() +" ： "+id);

            Poem poem = new Poem();
            poem.setId(id);


            //author name
            Element nameEle = document.getElementById("middlediv").getElementsByClass("zhuti").get(0).getElementsByTag("h2").get(0);
            poem.setTitle(StringUtil.getPoemName(nameEle.text()));

            Elements jjzz = document.getElementsByClass("jjzz");

            if(jjzz.size()>0) {
                //dynastyURL
                String dynastyURL =jjzz.get(0).getElementsByAttributeValueContaining("href", "/category/").attr("href");
                poem.setDynasty(StringUtil.getDynasty(dynastyURL));
                poem.setDynastyReal(this.dynastys.get(poem.getDynasty()));

                Elements zuozhe = jjzz.get(0).getElementsByAttributeValueContaining("href", "/chaxun/zuozhe/");
                //poems total
                String author = zuozhe.text();
                poem.setPoetName(author);

                poem.setPoetId(StringUtil.getAuthID(zuozhe.attr("href")));

//                String title = jjzz.get(0).getElementsByTag("h2").get(0).text();
//
//                poem.setTitle(StringUtil.getPoemName(title));

            }

            //introduction
            Elements zuozhe = document.getElementsByClass("zuozhe");


            //header img
            Element content = document.getElementById("shicineirong");
            poem.setContent(content.html().replace("<br />",""));
            if(poem.getContent().startsWith("诗句：")){
                poem.setIsShiju(1);
                Elements a = content.getElementsByAttributeValueStarting("href", "/chaxun/list");
                if(a.size()!=0){
                    poem.setFromPoemId(StringUtil.getAuthIntID(a.get(0).attr("href")));
                    poem.setFromPoemName(a.get(0).text());
                }

            }

            String intro= document.getElementsByClass("shangxi").html();
            poem.setIntroduction(intro.replace("<br />",""));

            //labels
            Elements labels = document.getElementById("middlediv").getElementsByClass("listscmk");
            if(labels.size() !=0){
                Elements as = labels.get(0).getElementsByTag("a");

                String labelsContent="";
                for (Element a : as) {
                    labelsContent+=","+a.text();
                }

                poem.setLabels(labelsContent.substring(1));
            }

            try {
                addPoem(poem);
            }catch (Exception e){
                Poem poem1 = new Poem();
                poem1.setId(id);
                poem1.setMemo("save_introduction_error");
                addPoem(poem1);
                logger.warn("save_introduction_error id :"+id);
            }

        }
    }

    /**
     * 添加一个诗人
     * @param poet
     * @throws Exception
     */
    public void addPoem(Poem poet) throws Exception {

        if (! poemsRepository.exists(poet.getId())){
            poemsRepository.saveAndFlush(poet);
        }

        logger.info(String.format("ID:%s, Title: %s, Author: %s,FromAuth",poet.getId(),poet.getTitle(),poet.getPoetName(),poet.getFromPoemName()));

    }

}
